using System.Collections.Generic;
using UnityEngine;

namespace Brick
{
	internal class gdk_debugConsole : MonoBehaviour
	{
		private struct Log
		{
			public string Message;

			public string StackTrace;

			public LogType LogType;
		}

		public static gdk_debugConsole Instance;

		[Tooltip("快捷键-开/关控制台")]
		public KeyCode ShortcutKey = KeyCode.Q;

		[Tooltip("摇动开启控制台？")]
		public bool ShakeToOpen;

		[Tooltip("窗口打开加速度")]
		public float shakeAcceleration = 3f;

		[Tooltip("是否保持一定数量的日志")]
		public bool restrictLogCount;

		[Tooltip("最大日志数")]
		public int maxLogs = 1000;

		private readonly List<Log> logs = new List<Log>();

		private Log log;

		private Vector2 scrollPosition;

		private bool visible;

		public bool collapse;

		private static readonly Dictionary<LogType, Color> logTypeColors = new Dictionary<LogType, Color>
	{
		{
			LogType.Assert,
			Color.white
		},
		{
			LogType.Error,
			Color.red
		},
		{
			LogType.Exception,
			Color.red
		},
		{
			LogType.Log,
			Color.white
		},
		{
			LogType.Warning,
			Color.yellow
		}
	};

		private const string ChinarWindowTitle = "Chinar-控制台";

		private const int Edge = 20;

		private readonly GUIContent clearLabel = new GUIContent("清空", "清空控制台内容");

		private readonly GUIContent hiddenLabel = new GUIContent("合并信息", "隐藏重复信息");

		private readonly Rect titleBarRect = new Rect(0f, 0f, 10000f, 20f);

		private Rect windowRect = new Rect(20f, 20f, Screen.width - 40, Screen.height - 40);

		private void OnEnable()
		{
			Application.logMessageReceived += HandleLog;
			Instance = this;
		}

		private void OnDisable()
		{
			Application.logMessageReceived -= HandleLog;
			Instance = null;
		}

		public void Show()
		{
			visible = !visible;
		}

		private void Update()
		{
			if (UnityEngine.Input.GetKeyDown(ShortcutKey))
			{
				visible = !visible;
			}
			if (ShakeToOpen && Input.acceleration.sqrMagnitude > shakeAcceleration)
			{
				visible = true;
			}
		}

		private void OnGUI()
		{
			if (visible)
			{
				windowRect = GUILayout.Window(666, windowRect, DrawConsoleWindow, "Chinar-控制台");
			}
		}

		private void DrawConsoleWindow(int windowid)
		{
			DrawLogsList();
			DrawToolbar();
			GUI.DragWindow(titleBarRect);
		}

		private void DrawLogsList()
		{
			scrollPosition = GUILayout.BeginScrollView(scrollPosition);
			for (int i = 0; i < logs.Count; i++)
			{
				if (collapse && i > 0)
				{
					Log log = logs[i];
					string message = log.Message;
					Log log2 = logs[i - 1];
					if (message != log2.Message)
					{
						continue;
					}
				}
				Dictionary<LogType, Color> dictionary = logTypeColors;
				Log log3 = logs[i];
				GUI.contentColor = dictionary[log3.LogType];
				Log log4 = logs[i];
				GUILayout.Label(log4.Message);
			}
			GUILayout.EndScrollView();
			GUI.contentColor = Color.white;
		}

		private void DrawToolbar()
		{
			GUILayout.BeginHorizontal();
			if (GUILayout.Button(clearLabel))
			{
				logs.Clear();
			}
			collapse = GUILayout.Toggle(collapse, hiddenLabel, GUILayout.ExpandWidth(expand: false));
			GUILayout.EndHorizontal();
		}

		private void HandleLog(string message, string stackTrace, LogType type)
		{
			logs.Add(new Log
			{
				Message = message,
				StackTrace = stackTrace,
				LogType = type
			});
			DeleteExcessLogs();
		}

		private void DeleteExcessLogs()
		{
			if (restrictLogCount)
			{
				int num = Mathf.Max(logs.Count - maxLogs, 0);
				MonoBehaviour.print(num);
				if (num != 0)
				{
					logs.RemoveRange(0, num);
				}
			}
		}
	}
}

